package com.time.workshop.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.time.workshop.API;
import com.time.workshop.App;
import com.time.workshop.BaseConstant;
import com.time.workshop.BaseFragment;
import com.time.workshop.R;
import com.time.workshop.adapter.TimeHallAdapter;
import com.time.workshop.bean.TimeHallList;
import com.time.workshop.injector.Injector;
import com.time.workshop.injector.V;
import com.time.workshop.ui.AddTaskActivity;
import com.time.workshop.ui.LoginActivity;
import com.time.workshop.ui.MyTaskListActivity;
import com.time.workshop.ui.TimeHallDetailActivity;
import com.time.workshop.utils.CommonUtils;
import com.time.workshop.utils.Log;
import com.time.workshop.utils.NewToast;
import com.time.workshop.utils.PreferenceUtil;
import com.time.workshop.view.RefreshViewLoadMoreProxy.OnLoadDataActionListener;
import com.time.workshop.view.RefreshViewLoadMoreProxy;
import com.time.workshop.view.TabHeadView;

/**
 * @author niemin 时间大厅
 */
public class TimeHallFragment extends BaseFragment implements OnClickListener,OnLoadDataActionListener
{

	@V
	private TabHeadView bv_time_hall_back;
	@V
	private RelativeLayout order_empty_relat;
	@V
	private ImageView fabuxuqiu;

	private TimeHallAdapter mAdapter;
	private List<TimeHallList> mList = new ArrayList<TimeHallList>();
	private PullToRefreshListView mRefreshListView;
	private RefreshViewLoadMoreProxy mLoadMoreProxy;
	private int pages = 1;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		return inflater.inflate(R.layout.activity_time_hall, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState)
	{
		super.onViewCreated(view, savedInstanceState);
		Injector.getInstance().inject(getActivity(), this, view);
		fabuxuqiu.setOnClickListener(this);
		init(view);
		getData();
	}

	private void init(View view)
	{
		bv_time_hall_back.getLeftButton().setBackgroundResource(R.drawable.btn_title_left_back_selector);
		bv_time_hall_back.getTitle().setText(getResources().getString(R.string.time_hall));
		bv_time_hall_back.getRightButton().setVisibility(View.VISIBLE);
		bv_time_hall_back.getRightButton().setText(getResources().getString(R.string.my_bangdan));
		bv_time_hall_back.getRightButton().setTextColor(getResources().getColor(R.color.color_24B6FF));
		bv_time_hall_back.getRightButton().setBackgroundColor(getResources().getColor(R.color.white));
		bv_time_hall_back.getLeftButton().setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				mSuperActivity.menu.showMenu();
			}
		});
		bv_time_hall_back.getRightButton().setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(getActivity(), MyTaskListActivity.class);
				startActivity(intent);
			}
		});

		mRefreshListView = (PullToRefreshListView) view.findViewById(R.id.time_hall_list);
		mRefreshListView.setEmptyView(order_empty_relat);
		mRefreshListView.getRefreshableView().setSelector(new ColorDrawable(Color.TRANSPARENT));
		mLoadMoreProxy = new RefreshViewLoadMoreProxy(mRefreshListView, Mode.BOTH,0,0);
		mLoadMoreProxy.setOnLoadDataActionListener(TimeHallFragment.this);
		mRefreshListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				Intent intent = new Intent(getActivity(), TimeHallDetailActivity.class);
				intent.putExtra("ID", mList.get(position-1).getID());
				intent.putExtra("HEADER_IMAGE", mList.get(position-1).getHEADER_IMAGE());
				startActivity(intent);
			}

		});
	}

	@Override
	public void onClick(View view)
	{
		switch (view.getId())
		{
		case R.id.fabuxuqiu:
			if (null == PreferenceUtil.getString(getActivity(), BaseConstant.PREF_TOKE_STRING))
			{
				Intent in = new Intent(getActivity(), LoginActivity.class);
				mSuperActivity.startActivity(in, true);
				return;
			}
			Intent in = new Intent(getActivity(), AddTaskActivity.class);
			mSuperActivity.startActivity(in, true);
			break;

		default:
			break;
		}
	}

	private void getData() {
		if (!CommonUtils.isNetworkConnected(getActivity())) {
			NewToast.makeText(getActivity(), getResources().getString(R.string.M000));
			return;
		}
		mSuperActivity.showDialog(null);
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("page", pages+"");

		HttpUtils httpUtils = new HttpUtils();

		httpUtils.send(HttpMethod.GET, API.task_list, params, new RequestCallBack<String>()
				{

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo)
			{
				Log.d("onSuccess==", responseInfo.result + "");

				mSuperActivity.dismissLoadingDialog();
				try
				{
					JSONObject jsonObject = new JSONObject(responseInfo.result);
					boolean isSuccess = jsonObject.getBoolean("success");
					if (!isSuccess) {
						NewToast.makeText(getActivity(),"获取数据失败");
						return;
					}
					JSONArray json = jsonObject.getJSONArray("result");
					Gson gson = new Gson();
					
					List<TimeHallList> list = gson.fromJson(json.toString(), new TypeToken<List<TimeHallList>>() {
					}.getType());
					
					int select = mList.size();
					if (list.size() != 0) {
						mList.addAll(list);
					}
					
					mAdapter = new TimeHallAdapter(getActivity(), mList);
					mRefreshListView.setAdapter(mAdapter);
					mRefreshListView.setSelection(select);
					mRefreshListView.onRefreshComplete();
				} catch (JSONException e)
				{
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(HttpException error, String msg)
			{
				mSuperActivity.dismissLoadingDialog();
				Log.d("onFailure==", msg+"");
				NewToast.makeText(getActivity(), R.string.re_loading_failed);
			
			}

				});
	}

	@Override
	public void onRefresh(int page) {
		mList.clear();
		pages = 1;
		getData();
	}

	@Override
	public void onLoadMore(int page) {
		pages++;
		getData();
	}

}
